﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKSparseDistance.Properties;
using YJKGeometryFuncs;
using YJKInspectModelFunc.ModelLayer;

namespace YJKSparseDistance.ModelLayer
{
	internal class DoorCurveBean : BaseCurveBean
	{
		public Curve ThisBaseCurve
		{
			get
			{
				return this.m_thisBaseCurve;
			}
		}

		public List<Curve> RealUseCurves
		{
			get
			{
				return this.m_RealUseCurves;
			}
			set
			{
				this.m_RealUseCurves = value;
			}
		}

		public BaseDoorPacker ThisPacker
		{
			get
			{
				return this.m_thisPacker;
			}
		}

		public int ThisPackerId
		{
			get
			{
				return this.m_thisPackerId;
			}
		}

		public int ThisElementId
		{
			get
			{
				return this.m_thisElementId;
			}
		}

		public int ThisLinkInstanceId
		{
			get
			{
				return this.m_thisLinkInstanceId;
			}
		}

		public Curve CurveOffestInRoom
		{
			get
			{
				return this.m_CurveOffestInRoom;
			}
		}

		public DoorCurveBean(Curve baseDoorCurve, BaseDoorPacker usePacker, Curve offestCurve = null)
		{
			this.m_CurveOffestInRoom = offestCurve;
			this.m_thisBaseCurve = baseDoorCurve;
			this.m_RealUseCurves = new List<Curve>
			{
				baseDoorCurve
			};
			this.m_thisPacker = usePacker;
			this.m_thisPackerId = usePacker.ThisElementPackerId;
			this.m_thisElementId = usePacker.ElementId;
			this.m_thisLinkInstanceId = usePacker.LinkInstanceId;
			foreach (Curve inputCurve in this.m_RealUseCurves)
			{
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(inputCurve, this.m_thisPacker, false, false, false, false));
			}
		}

		public override List<BulidingCurvePakcer> LstCurvePacker
		{
			get
			{
				List<BulidingCurvePakcer> list = new List<BulidingCurvePakcer>();
				list.AddRange(this.m_lstCurvePacker);
				int elementPackerId = -1;
				int linkInstacneId = -1;
				if (this.m_thisPacker.ThisDoor.Host != null)
				{
					elementPackerId = this.m_thisPacker.ThisDoor.Host.Id.IntegerValue;
				}
				if (this.m_thisPacker.UseRevitLinkInstance != null)
				{
					linkInstacneId = this.m_thisPacker.UseRevitLinkInstance.Id.IntegerValue;
				}
				if (null != this.m_CurveOffestInRoom)
				{
					list.Add(new BulidingCurvePakcer(this.m_CurveOffestInRoom, elementPackerId, linkInstacneId, true, true, true, true));
				}
				this.m_lstAllFreeEnd = base.GetFreeCurveFreeEnd(list);
				return list;
			}
		}

		public bool CutThisDoor(DoorCurveBean inputValue)
		{
			if (this == inputValue)
			{
				return false;
			}
			if (this.m_thisElementId != inputValue.m_thisElementId || this.m_thisLinkInstanceId != inputValue.m_thisLinkInstanceId)
			{
				return false;
			}
			if (this.m_bIfHasCuted)
			{
				return false;
			}
			this.m_RealUseCurves = new List<Curve>();
			this.m_lstCurvePacker = new List<BulidingCurvePakcer>();
			inputValue.m_RealUseCurves = new List<Curve>();
			inputValue.m_lstCurvePacker = new List<BulidingCurvePakcer>();
			foreach (Curve inputCurve in Utility.MakeEndCurves(this.m_thisBaseCurve, inputValue.m_thisBaseCurve))
			{
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(inputCurve, this.m_thisPacker, false, false, false, false));
			}
			List<XYZ> list2;
			List<Curve> list = Utility.CutCurveFromMid(this.m_thisBaseCurve, double.Parse(Resources.Str_MidCutFactory), out list2);
			List<XYZ> list4;
			List<Curve> list3 = Utility.CutCurveFromMid(inputValue.m_thisBaseCurve, double.Parse(Resources.Str_MidCutFactory), out list4);
			foreach (Curve inputCurve2 in list)
			{
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(inputCurve2, this.m_thisPacker, true, false, false, false));
			}
			foreach (Curve inputCurve3 in list3)
			{
				inputValue.m_lstCurvePacker.Add(new BulidingCurvePakcer(inputCurve3, this.m_thisPacker, true, false, false, false));
			}
			if (Geometry.LessThan(Utility.DistanceNonZValue(list2[1], list4[0]), Utility.DistanceNonZValue(list2[0], list4[0])))
			{
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[1], list4[0]), this.m_thisPacker, true, true, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[0], list4[1]), this.m_thisPacker, true, true, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[1], list4[1]), this.m_thisPacker, true, false, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[0], list4[0]), this.m_thisPacker, true, false, false, false));
			}
			else
			{
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[0], list4[0]), this.m_thisPacker, true, true, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[1], list4[1]), this.m_thisPacker, true, true, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[1], list4[0]), this.m_thisPacker, true, false, false, false));
				this.m_lstCurvePacker.Add(new BulidingCurvePakcer(Line.CreateBound(list2[0], list4[1]), this.m_thisPacker, true, false, false, false));
			}
			this.m_bIfHasCuted = true;
			inputValue.m_bIfHasCuted = true;
			return true;
		}

		private bool m_bIfHasCuted;

		private Curve m_thisBaseCurve;

		private List<Curve> m_RealUseCurves;

		private BaseDoorPacker m_thisPacker;

		private int m_thisPackerId;

		private int m_thisElementId;

		private int m_thisLinkInstanceId;

		private Curve m_CurveOffestInRoom;
	}
}
